package com.example.himalayaproject.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.himalayaproject.R;
import com.example.himalayaproject.fragment.DownloadFragment;
import com.example.himalayaproject.fragment.FindFragment;
import com.example.himalayaproject.fragment.OwnerFragment;
import com.example.himalayaproject.fragment.PlayFragment;
import com.example.himalayaproject.fragment.ScriptionFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private RadioGroup group;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private List<Fragment> fragments;
    private ImageView play;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        aboutRadioButton();
        aboutFragmentManger();
    }


    private void aboutRadioButton() {
        group = ((RadioGroup) findViewById(R.id.main_group));
        play = (ImageView) findViewById(R.id.main_play_button);
        group.setOnCheckedChangeListener(this);
        play.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        transaction = fragmentManager.beginTransaction();
        switch (i) {
            case R.id.main_find_button:
                transaction.replace(R.id.main_fragment_container, fragments.get(0));
                break;
            case R.id.main_subscription_button:
                transaction.replace(R.id.main_fragment_container, fragments.get(1));
                break;
            case R.id.main_play_button:
                transaction.replace(R.id.main_fragment_container, fragments.get(2));
                break;
            case R.id.main_download_button:
                transaction.replace(R.id.main_fragment_container, fragments.get(3));
                break;
            case R.id.main_owner_button:
                transaction.replace(R.id.main_fragment_container, fragments.get(4));
                break;
        }
        transaction.commit();
    }

    private void aboutFragmentManger() {
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        FindFragment findFragment = new FindFragment();
        DownloadFragment downloadFragment = new DownloadFragment();
        OwnerFragment ownerFragment = new OwnerFragment();
        PlayFragment playFragment = new PlayFragment();
        ScriptionFragment subScriptionFragment = new ScriptionFragment();
        fragments = new ArrayList<>();
        fragments.add(findFragment);
        fragments.add(subScriptionFragment);
        fragments.add(playFragment);
        fragments.add(downloadFragment);
        fragments.add(ownerFragment);
        transaction.add(R.id.main_fragment_container, fragments.get(0));
        transaction.add(R.id.main_fragment_container, fragments.get(1));
        transaction.add(R.id.main_fragment_container, fragments.get(2));
        transaction.add(R.id.main_fragment_container, fragments.get(3));
        transaction.add(R.id.main_fragment_container, fragments.get(4));
        transaction.replace(R.id.main_fragment_container, fragments.get(0));
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        transaction = fragmentManager.beginTransaction();
        switch (v.getId()) {
            case R.id.main_play_button:
                transaction.replace(R.id.main_fragment_container, fragments.get(2));
                Toast.makeText(this, "play", Toast.LENGTH_SHORT).show();
                break;
        }
        transaction.commit();
    }

}

/*
* java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
* {com.example.himalayaproject/com.example.himalayaproject.activity.MainActivity}:
 * java.lang.ClassNotFoundException: Didn't find class
  * "com.example.himalayaproject.activity.MainActivity"
  * on path: DexPathList[[zip file "/data/app/com.example.himalayaproject-276.apk"],
  * nativeLibraryDirectories=[/data/app-lib/com.example.himalayaproject-276, /vendor/lib, /system/lib]]
                                                                                 */